package com.cxs.dialogdemo.slice;

import com.cxs.dialogdemo.ResourceTable;
import com.cxs.dialogdemo.component.CustomComponent;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.CommonDialog;

public class MainAbilitySlice extends AbilitySlice {
    Text textHello;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        textHello = (Text)findComponentById(ResourceTable.Id_text_helloworld);

        findComponentById(ResourceTable.Id_text_helloworld)
                .setClickedListener(c -> {
                    showDialog();
                });

    }

    private void showDialog() {
        CommonDialog commonDialog = new CommonDialog(this);
        CustomComponent customComponent = new CustomComponent(this);
        customComponent.setDialogCallback(new CustomComponent.DialogCallback() {
            @Override
            public void result(String date) {
                textHello.setText(date);
                commonDialog.remove();
            }
        });
        commonDialog.setContentCustomComponent(customComponent);
        commonDialog.show();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
